package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理相册相关的请求
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "04.类别管理模块")
@Slf4j
@RequestMapping("/categorys")
@RestController
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;
    public CategoryController(){
        log.debug("创建控制器对象: CategoryController");
    }
    //http://localhost:9080/categorys/addnew?name=类别015&parentId=12&depth=21&keywords=p&sort=166&icon=pic&enable=0&isParent=11&isDisplay=10
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addnew")
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理[添加类别]的请求,参数:{}",categoryAddNewDTO);
            categoryService.addNew(categoryAddNewDTO);
            log.debug("测试添加数据成功!");
            return JsonResult.ok();
    }

    //http://localhost:9080/categorys/9527/delete
    @ApiOperation("根据id删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "类别id",required = true,dataType = "long")
    @PostMapping( "/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除类别失败，尝试删除的类别的ID无效！")
                                   @PathVariable Long id){
        log.debug("开始处理[根据id删除类别]的请求,参数: {}", id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    //http://localhost:9080/categorys/9527/update
    @ApiOperation("根据id修改类别详情")
    @ApiOperationSupport(order = 300)
    @PostMapping( "/update")
    public JsonResult<Void> updateById(@Valid CategoryUpdateDTO categoryUpdateDTO){
        log.debug("开始处理[根据id修改类别详情]的请求,参数: {}", categoryUpdateDTO.getId());
        categoryService.updateById(categoryUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/categorys/9527
    @ApiOperation("根据id查询类别详情")
    @ApiOperationSupport(order = 419)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CategoryStandardVO> getStandardById( @PathVariable Long id) {
        log.debug("开始处理【根据id查询类别详情】的请求，参数: {}", id);
        CategoryStandardVO queryResult = categoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/categorys
    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> list() {
        log.debug("开始处理【查询类别列表】的请求，无参数");
        List<CategoryListItemVO> list = categoryService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/categorys/list-by-parent?parentId=0
    @ApiOperation("根据父级类别查询其子集类别列表")
    @ApiOperationSupport(order = 415)
    @GetMapping("/list-by-parent")
    public JsonResult<List<CategoryListItemVO>> listByParentId(Long parentId) {
        log.debug("开始处理【根据父级类别查询其子集类别列表】的请求，无参数");
        List<CategoryListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    // http://localhost:8080/categorys/9527/enable
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1, message = "启用类别失败，尝试启用的类别的ID无效！")
                                      @PathVariable Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:8080/categorys/9527/disable
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@Range(min = 1, message = "禁用类别失败，尝试禁用的类别的ID无效！")
                                       @PathVariable Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }
}
